package genealogy.deprecated.datamodel.pedigree;

import genealogy.deprecated.datamodel.pedigree.visitors.IPedigreeVisitor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Supports two types of locations:<ol>
 *  <li>A well formatted list of for location sub-parts in increasing specifity order</li>
 *  <li>A single string representing a location (will loose some accuracy)</li>
 * </ol>
 * @author Steve
 */
@Deprecated
public class Location implements Serializable , IPedigreeVisitable{
	
	//serialVersionUID
	private static final long serialVersionUID = 3563494715883663323L;
	
	private static final int MAX_LOCATIONS = 4;
	private static final String DELIM = ",";

	private String[] m_locations = null;

	/**
	 * Constructor
	 * 
	 * @param fullLocation
	 * @param locations
	 */
	public Location(String unparsedLocation) {
		super();
		m_locations = new String[1];
		m_locations[0] = unparsedLocation;
	}

	/**
	 * Constructor Takes in up to four locations in increasing order of
	 * specifity, ex: Country, State/Province, city/townshp,
	 * cemetary/hospital/etc
	 * 
	 * @param locations
	 * @throws PedigreeValidationError
	 */
	public Location(List<String> parsedLocations) throws PedigreeValidationError {
		if (parsedLocations != null && parsedLocations.size() > 0)
		{
			if (parsedLocations.size() > MAX_LOCATIONS)
			{
				throw new PedigreeValidationError(
						"Parses locations must follow the pattern: Country,"
								+ " State/Province, city/townshp, cemetary/hospital/etc ");
			}
			m_locations = new String[parsedLocations.size()];
			for (int i = 0; i < parsedLocations.size(); i++)
			{
				m_locations[i] = parsedLocations.get(i);
			}
		}
	}

	/**
	 * @return the locations
	 */
	public List<String> getLocations() {
		List<String> locations = new ArrayList<String>();
		if(m_locations != null){
			for(String location : m_locations){
				locations.add(location);
			}
		}
		return locations;
	}

	/**
	 * @return the if the location is parsed into sub locations
	 */
	public boolean isParsed() {
		return m_locations != null && m_locations.length > 0 && !m_locations[0].contains(DELIM );
	}	
	
	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString(){
/*		StringBuilder result = new StringBuilder();
		if(m_locations != null){
			for(String location : m_locations){
				result.append(m_locations[i]);
				if(i != 1){
					result.append(", ");

			}
		}
		return result.toString();*/
		//FIXME: so it works
		return m_locations[0];
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object arg0) {
		if(arg0 instanceof Location){
			Location l = (Location) arg0;
			return Arrays.deepEquals(this.m_locations, l.m_locations);
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see genealogy.datamodel.pedigree.IPedigreeVisitable#accept(genealogy.datamodel.pedigree.visitors.AbstractPedigreeVisitor)
	 */
	public void accept(IPedigreeVisitor visitor) {
		visitor.visit(this);
		visitor.endVisit();
	}
}
